
--添加一个边框面板
function class.ui_base.add_frame(self,frame,...)
    local params = {...}
    local x,y,w,h = table.unpack(params)
    if #params==1 then
        local size = params[1]
        w = self.w * size
        h = self.h * size
        x = (self.w - w)/2
        y = (self.h - h)/2
    end
    local tool = class.panel:builder
    {
        parent = self,
        fdf = frame,
        x = x,
        y = y,
        w = w,
        h = h,
    }
    return tool
end

--添加一个数量下标
function class.ui_base.add_number_panel(self,...)
    local params = {...}
    local x,y,w,h,size = table.unpack(params)
    w = w or 36
    h = h or 32
    if #params==1 then
        local size = params[1]
        w = 36 * size
        h = 32 * size
    end
    x = x or self.w - w
    y = y or self.h - h
    local tool = class.panel:builder
    {
        parent = self,
        type = 'panel',
        normal_image = [[UI\Widgets\Console\Human\CommandButton\human-button-lvls-overlay.blp]],
        x = x,
        y = y,
        w = w,
        h = h,
        text = {
            type = 'text',
            text = '',
            align = 5,
            justH = 'CENTER',
            justV = 'MIDDLE',
            outline = true,
            font_size = size or 10,
        }
    }
    return tool
end


--添加一个带名字的按钮
function class.ui_base.add_namebutton(self,name,x,y,w,h,size,align)
    local button = self:add_button('',x,y,w,h,1)
    button:add_frame('tooltip_backdrop1')
    button.name = name
    button.hover_image = ''
    button.text = button:add_text(name,0,0,nil,nil,size or 12,5)
    return button
end


--[翻页区域]
function class.ui_base.add_page_area(self,x,y,w,h)
    local w0,h0 = 16,14 --翻页按钮大小
    local w1 = 80   --页码显示大小
    local x = x or (self.w - 2*w0 - w1)/2
    local y = y or (self.h - 45)
    local w = (2*w0 + w1)
    local h = h0
    local panel = self:add_panel('',x,y,w,h)
    local art1_1 = [[ui\吞噬面板\左翻页-常态.tga]]
    local art1_2 = [[ui\吞噬面板\左翻页-亮.tga]]
    local art1_3 = [[ui\吞噬面板\左翻页-禁用.tga]]
    local art2_1 = [[ui\吞噬面板\右翻页-常态.tga]]
    local art2_2 = [[ui\吞噬面板\右翻页-亮.tga]]
    local art2_3 = [[ui\吞噬面板\右翻页-禁用.tga]]
    local page_los = panel:add_button(art1_3,0,0,w0,h0)
    local page_add = panel:add_button(art2_3,w0+w1,0,w0,h0)
    page_los:set_pushedd(art1_2)
    page_add:set_pushedd(art2_2)
    page_los:set_highlight(art1_1)
    page_add:set_highlight(art2_1)

    self.page_text = panel:add_text('1/1',w0,0,w1,h0,10,5)
    self.page = 1
    self.max_page = 1
    self.page_los = page_los
    self.page_add = page_add
    return panel
end





function class.ui_base.play_animation(self)
    if self.animation_icon==nil then 
        return
    end
    if self._timer then 
        self._timer:remove()
        self._timer = nil
    end 
    local num = 0
    self._timer = ac.loop(33,function (timer)
        num = num + 1
        if (num > self.animation_count) or self.handle == nil or self.handle == 0 then 
            self.animation_icon:set_normal_image('')
            timer:remove()
            return
        end 
        local str = string.format(self.animation_path,num)
        self.animation_icon:set_normal_image(str)
    end)
end


local move_animation = {}
function class.ui_base.move_animation(self,target,duration,easing)
    local easing = easing or '线性'
    local x1,y1 = self:get_real_position()
    local w1,h1 = self.w,self.h
    local x2,y2 = target[1] or x1,target[2] or y1
    local w2,h2 = target[3] or w1,target[4] or h1
    local start = {x1,y1,w1,h1}
    local target = {x2,y2,w2,h2}
    self.tween = ac.tween(easing,duration)
    self.tween:begin(start)
    self.tween:after(target)
    move_animation[self.handle] = self
end



ac.hardware:on "界面绘制" (function(_,t)
    for id,panel in pairs(move_animation) do
        local tween = panel.tween
        local point = tween:update(t)
        panel:set_real_position(point[1],point[2])
        panel:set_control_size(point[3],point[4])
        panel:notify('move_animationing',tween)
        if tween.clock >= tween.duration then -- the tween has expired
            move_animation[id] = nil
            panel:notify('move_animation_finish')
        end
    end
end)